<template>
  <div class="header">
    <div class="music">
      <div :class="['touxiang', { active: active }]"></div>
      <span @click="toggle" :class="['icon', 'iconfont', active ? music.iconKaiShi : music.iconJieSu]"></span>
      <span class="name">{{ music.name }}</span>
      <audio ref="music" :src="music.musicMp3" loop="loop"></audio>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'

export default {
  name: 'v-header',
  data () {
    return {
      active: true
    }
  },
  methods: {
    // 点击切换
    toggle () {
      if (this.active) {
        // 开始播放
        this.$refs.music.play()
      } else {
        // 暂停播放
        this.$refs.music.pause()
      }
      // 切换active
      this.active = !this.active
    }
  },
  computed: {
    // music 数据
    ...mapState({
      music: (state) => state.music
    })
  }
}
</script>

<style lang="less" scoped>
.header {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 999;
  .music {
    position: absolute;
    left: 30px;
    top: 30px;
    width: 240px;
    height: 62px;
    border-radius: 8px;
    padding: 0 10px;
    box-sizing: border-box;
    border: 1px solid rgba(0, 0, 0, .4);
    background-color: rgba(255, 255, 255, 0.6);
    display: flex;
    align-items: center;
    .touxiang {
      width: 60px;
      height: 60px;
      border-radius: 50%;
      box-sizing: border-box;
      border: 1px solid rgba(0, 0, 0, .1);
      background: url(../../assets/images/touxiang.jpg) no-repeat center center;
      background-size: 110% 110%;
      position: relative;
      overflow: hidden;
      animation: rotate 20s linear infinite;
      &.active {
        animation-play-state: paused;
      }
    }
    .icon {
      position: absolute;
      cursor: pointer;
      left: 10px;
      top: 0;
      width: 60px;
      height: 60px;
      border-radius: 50%;
      opacity: 0;
      background-color: rgba(0, 0, 0, .3);
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 30px;
      transition: opacity .5s linear;
      &:hover {
        opacity: 1;
      }
    }
    .name {
      background-color: black;
      display: block;
      margin-left: 20px;
      font-size: 16px;
      height: 30px;
      line-height: 30px;
      letter-spacing: 3px;
      padding: 0 10px;
      border-radius: 5px;
      color: white;
      cursor: pointer;
    }
  }
}

@keyframes rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

@media screen and (max-width: 600px) {
  .header {
    .music {
      width: 100vw;
      height: 50px;
      border: 0;
      border-radius: 0;
      top: 0;
      left: 0;
      background-color: #080908;
      .touxiang {
        width: 40px;
        height: 40px;
      }
      .icon {
        width: 40px;
        height: 40px;
        top: 5px;
        font-size: 16px;
        z-index: 2;
      }
      .name {
        width: 100%;
        height: 100%;
        position: absolute;
        display: flex;
        justify-content: center;
        align-items: center;
        top: 0;
        left: 0;
        background-color: transparent;
        margin: 0;
      }
    }
  }
}
</style>
